package edu.sjsu.cs.queryopt.dbms.main;

import java.text.NumberFormat;
import java.util.ArrayList;

public class TableHelper {

	/**
	 *  CHAR = 1  Value should be next
	 *  INT =  2  Value should be -1
	 * 
	 * **/
	public static ArrayList<Integer> checkColTypeAndValue(String firstRow)
	{
	//	int [ ] valueAndType = new int [Helper.HowManyColInTable(firstRow)*2];
		ArrayList<Integer> valueAndType = new ArrayList<Integer>();
		String[] tokens = firstRow.split("\\s+");
		int index;
		int valueIndex = 0 ;
//		for(int i = 0 ; i < tokens.length ; i++)
//		{
//			System.out.println("i" + i + " " + tokens[i]);
//		}
		
		for(index = 0; index < tokens.length ; index++)
		{
		//	System.out.println("(tokens[index].charAt(0) " + (tokens[index].charAt(0)));
			if(tokens[index].charAt(0) == WordCapsule.QWC_CHAR)
			{
				valueAndType.add(1);
				if(checkValueForCol((String) tokens[index].subSequence(1, tokens[index].length()))
						== -99)
				{
					break;
					
				}
				else
				{
					valueAndType.add(
							checkValueForCol((String) tokens[index].subSequence(1, tokens[index].length())));
				}
			}
			else if(tokens[index].charAt(0) == WordCapsule.QWC_INT)
			{
				valueAndType.add(2);
				valueAndType.add(10);
			}
			else
			{
				
				System.out.println("Error : checkColTypeAndValue  we only have int and char type");
			}

		}
		return valueAndType;
				
	}
	public static int checkValueForCol(String coltypewithvalue)
	{
	//	System.out.println("String value? " + coltypewithvalue);
		try{
			return Integer.parseInt(coltypewithvalue);

			}catch(NumberFormatException e){
				//System.out.println("Error: checkValueForCol this is not Int Val ");
				return -99;
			}
		
	}
	/**
	 *  CHAR = 1  Value should be next
	 *  INT =  2  Value should be -1
	 * 
	 * **/
	public static boolean checkEachRowTypeAndValue(ArrayList<Integer> typeAndValue , String oneRow)
	{
		
		ArrayList<String> StringArrayList = Helper.StringToArrayList(oneRow);
		ArrayList<Integer> TypeChecking = getAllTypeInTable(typeAndValue);
		ArrayList<Integer> SizeChecking = getAllSizeInTable(typeAndValue);
		int successorTracker = 0;
//		for(int index = 0 ; index < typeAndValue.size() ; index++)
//		{
//			System.out.println("index :" +index + "  " + typeAndValue.get(index));
//		}
//		for(int index = 0 ; index < StringArrayList.size() ; index++)
//		{
//			System.out.println("Contents :" +index + "  " + StringArrayList.get(index));
//			
//		}
//		


		
		return false;
	}
	
	/**
	 * index%2 = 0 will have type for cols
	 * 
	 * **/
	public static ArrayList<Integer> getAllTypeInTable(ArrayList<Integer> al)
	{
		ArrayList<Integer> TypeChecking = new ArrayList<Integer>();
		
		for(int index = 0; index < al.size() ; index++)
		{
			if((index%2 == 0))
			{
				TypeChecking.add(al.get(index));
				
			}
		}
		
		return TypeChecking;	
		
	}
	/**
	 * index%2 = 1 will have size for cols in table
	 * **/
	public static ArrayList<Integer> getAllSizeInTable(ArrayList<Integer> al)
	{
		ArrayList<Integer> SizeChecking = new ArrayList<Integer>();
		
		for(int index = 0; index < al.size() ; index++)
		{
			if((index%2 == 1))
			{
				SizeChecking.add(al.get(index));
				
			}
		}
		
		return SizeChecking;

	}
	/**
	 * CHeck it is int of not from string value
	 * **/
	
	public static boolean isInt(String intValue)
	{
		try{
			  int num = Integer.parseInt(intValue);
			  return true;
			} catch (NumberFormatException e) {
			  return false;
			}	
	}
	/**
	 * finish later
	 * **/
	public static boolean IsRightCharSize(String word, int charSize)
	{
			
		return true;
	}
	public static double ConvertStringToDouble(String StringIntValue)
	{
		return Double.parseDouble(StringIntValue);
	}
	/**
	 * @param dval : Double value with E
	 * Result : it reture string type double with out E 
	 * 
	 * **/
	public static String CovertDoubleValueWithOutE(double dval)
	{
		NumberFormat f = NumberFormat.getInstance();
		f.setGroupingUsed(false);
		String refinedNumber = f.format(dval);
		return refinedNumber;
	}
	/**
	 * 
	 * **
	 * @param pod; pod [X2, T2, 1A, 2D]
	 * @param order col that we want to read: 1A return 1 
	 * 										  2D return 2
	 * @return
	 */
	public static int returnColFromIndex (ArrayList<String> pod , int order)
	{
		
//		System.out.println("pod "+ pod);
//		System.out.println("order "+ order);
//		System.out.println("pod.get(order+1) "+ pod.get((order)));
//		System.out.println("pod.get(order+1)).charAt(1) "+ pod.get(order).charAt(0));
		
		int temp = (int)((pod.get(order+1)).charAt(0)) - 48;
	
		return temp;
	}
	/**
	 * ArrayList <Integer> al: valyetype [1, 6, 2, 10, 1, 5]
	 * 
	 * */
	public static ArrayList<Integer> getColValue(ArrayList <Integer> al)
	{
		ArrayList<Integer> re  =  new ArrayList<Integer>();
		for(int index =  0 ; index < al.size() ; index++)
		{
			if(index%2 == 0)
			{
				re.add(al.get(index));
			}
		}
		return re;
		
	}
	/**
	 * ArrayList <Integer> al: valyetype [1, 6, 2, 10, 1, 5]
	 * 
	 * */
	public static ArrayList<Integer> getColSize(ArrayList <Integer> al)
	{
		ArrayList<Integer> re  =  new ArrayList<Integer>();
		for(int index =  0 ; index < al.size() ; index++)
		{
			if(index%2 != 0)
			{
				re.add(al.get(index));
			}
		}
		return re;
		
	}
	
}
